// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

import { agentLogin, logout, getInfo } from '@/api/agent';
import { getAgentToken, setAgentToken,getAgentInfo,setAgentInfo } from '@/utils/auth';
import router, { resetRouter } from '@/router';
import { isLoginApi } from '@/api/sms';
import Cookies from 'js-cookie';
import { Loading } from 'element-ui';


const state = {
  token: getAgentToken(),
  agentInfo:getAgentInfo()
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  setInfo(state, val) {
    console.log(val)
    state.agentInfo = val;
  },
};

const actions = {
  login({ commit }, userInfo) {
    Loading.service();
    console.log(userInfo)
    return new Promise((resolve, reject) => {
      agentLogin(userInfo)
        .then((res) => {
          let loadingInstance = Loading.service();

          loadingInstance.close();
          // 记录用户信息
          commit('setInfo', JSON.stringify(res.agentInfo));
          setAgentInfo(res.agentInfo)
          // this.$store.commit('agent/SET_TOKEN')
          commit('SET_TOKEN', res.token);
          setAgentToken(res.token);
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};

